package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.PubNavigation;
import com.ruoyi.system.mapper.PubNavigationTypeMapper;
import com.ruoyi.system.domain.PubNavigationType;
import com.ruoyi.system.service.IPubNavigationTypeService;

/**
 * 导航分类Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-10
 */
@Service
public class PubNavigationTypeServiceImpl implements IPubNavigationTypeService
{
    @Autowired
    private PubNavigationTypeMapper pubNavigationTypeMapper;

    /**
     * 查询导航分类
     *
     * @param id 导航分类主键
     * @return 导航分类
     */
    @Override
    public PubNavigationType selectPubNavigationTypeById(Long id)
    {
        return pubNavigationTypeMapper.selectPubNavigationTypeById(id);
    }

    /**
     * 查询导航分类列表
     *
     * @param pubNavigationType 导航分类
     * @return 导航分类
     */
    @Override
    public List<PubNavigationType> selectPubNavigationTypeList(PubNavigationType pubNavigationType)
    {
        return pubNavigationTypeMapper.selectPubNavigationTypeList(pubNavigationType);
    }

    /**
     * 新增导航分类
     *
     * @param pubNavigationType 导航分类
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPubNavigationType(PubNavigationType pubNavigationType)
    {
        pubNavigationType.setCreateTime(DateUtils.getNowDate());
        int rows = pubNavigationTypeMapper.insertPubNavigationType(pubNavigationType);
        insertPubNavigation(pubNavigationType);
        return rows;
    }

    /**
     * 修改导航分类
     *
     * @param pubNavigationType 导航分类
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePubNavigationType(PubNavigationType pubNavigationType)
    {
        pubNavigationType.setUpdateTime(DateUtils.getNowDate());
        pubNavigationTypeMapper.deletePubNavigationByType(pubNavigationType.getId());
        insertPubNavigation(pubNavigationType);
        return pubNavigationTypeMapper.updatePubNavigationType(pubNavigationType);
    }

    /**
     * 批量删除导航分类
     *
     * @param ids 需要删除的导航分类主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePubNavigationTypeByIds(Long[] ids)
    {
        pubNavigationTypeMapper.deletePubNavigationByTypes(ids);
        return pubNavigationTypeMapper.deletePubNavigationTypeByIds(ids);
    }

    /**
     * 删除导航分类信息
     *
     * @param id 导航分类主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePubNavigationTypeById(Long id)
    {
        pubNavigationTypeMapper.deletePubNavigationByType(id);
        return pubNavigationTypeMapper.deletePubNavigationTypeById(id);
    }

    /**
     * 新增导航信息
     *
     * @param pubNavigationType 导航分类对象
     */
    public void insertPubNavigation(PubNavigationType pubNavigationType)
    {
        List<PubNavigation> pubNavigationList = pubNavigationType.getPubNavigationList();
        Long id = pubNavigationType.getId();
        if (StringUtils.isNotNull(pubNavigationList))
        {
            List<PubNavigation> list = new ArrayList<PubNavigation>();
            for (PubNavigation pubNavigation : pubNavigationList)
            {
                pubNavigation.setType( id);
                list.add(pubNavigation);
            }
            if (list.size() > 0)
            {
                pubNavigationTypeMapper.batchPubNavigation(list);
            }
        }
    }
}
